<?php

namespace app\admin\service;

use app\admin\dao\ConsultingQuestionAnswerDao;
use app\admin\dao\ConsultingQuestionDao;
use app\admin\model\UM as UM;
use app\common\model\ConsultingQuestion;
use ShidianUtil\Exception\DataNotExistException;
use ShidianUtil\Utils\TimeUtil;
use think\exception\ValidateException;

/**
 * Class QuestionService
 * @package app\admin\service
 */
class QuestionService
{
    // dao
    private ConsultingQuestionDao $consultingQuestionDao;

    public function __construct(ConsultingQuestionDao $consultingQuestionDao)
    {
        $this->consultingQuestionDao = $consultingQuestionDao;
    }

    /**
     * 列表
     * @param UM|null $user
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function index(?UM $user, $categoryId, $startTime, $endTime, $kw, ?int $page = null, ?int $page_size = null): array
    {
        if(is_numeric($endTime)){
            $endTime = TimeUtil::stamp(TimeUtil::DAY, $endTime, false);
        }
        return $this->consultingQuestionDao->listQuestions($categoryId, $startTime, $endTime, $kw, $page, $page_size);
    }

    /**
     * 查询
     * @param UM|null $user
     * @param int $id
     * @return ConsultingQuestion
     */
    public function read(?UM $user, int $id): ?ConsultingQuestion
    {
        return $this->consultingQuestionDao->findQuestion($id);
    }

    /**
     * 更新
     * @param UM $user
     * @param int $id
     * @param array $params
     */
    public function modify(UM $user, int $id, array $params): void
    {
        $record = ConsultingQuestion::field(true)->where('id', $id)->find();

        if (is_null($record)) {
            throw new DataNotExistException('该热门咨询不存在或已被删除');
        }

        $consultingArr = $params['consultingArr'] ?? [];
        if (empty($consultingArr)) {
            return;
        }

        $isSel = false;
        foreach ($consultingArr as $consulting) {
            $tmp = $consulting['questionAnswer'] ?? null;
            if ($tmp) {
                if ($tmp['isShow'] == '1') {
                    $isSel = true;
                }
            }
        }

        if (!$isSel) {
            throw new ValidateException('请在右侧勾选');
        }

        $lawyerId = null;
        $answer = null;

        foreach ($consultingArr as $consulting) {
            $tmp = $consulting['questionAnswer'] ?? null;
            if ($tmp) {

                if ($tmp['isShow'] == '1') {
                    $answer = $tmp['answer'];
                    $lawyerId = $consulting['lawyerId'];
                    $isSel = true;
                }
                $tmp['created'] = time();

                // 保存
                $this->getConsultingQuestionAnswerDao()->replaceActive($tmp);
            }
        }

        if (is_id($lawyerId)) {
            $record->lawyerId = $lawyerId;
            $record->answer = $answer;
            $record->isPublish = 1;
            $record->publishTime = time();
            $record->save();
        } else {
            $record->isPublish = 0;
            $record->save();
        }
    }

    private function getConsultingQuestionAnswerDao(): ConsultingQuestionAnswerDao
    {
        return app(ConsultingQuestionAnswerDao::class);
    }

}
